<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\models\User */
/* @var $form yii\widgets\ActiveForm */
?>

<?php $form = ActiveForm::begin([
    'id' => 'data-form',
    'options'=>['lay-filter'=>'form-data', 'class'=>'layui-form'],
    'fieldConfig' => [
        'options' => [
            'tag' => false, //不生成上一层的<div class='form-group'>
        ],
    ],
]); ?>
<div class="layui-form-item">
    <?= $form->field($model, 'username', [
        'template' => "{label}<div class=\"layui-input-inline\">{input}</div>",
        'labelOptions' => ['class'=>'layui-form-label'],
    ])->textInput(['class'=>'layui-input']) ?>
    <?= $form->field($model, 'email', [
        'template' => "{label}<div class=\"layui-input-inline\">{input}</div>",
        'labelOptions' => ['class'=>'layui-form-label'],
    ])->textInput(['class'=>'layui-input']) ?>
</div>
<div class="layui-form-item">
    <?= $form->field($model, 'password', [
        'template' => "{label}<div class=\"layui-input-inline\">{input}</div>",
        'labelOptions' => ['class'=>'layui-form-label'],
    ])->passwordInput(['class'=>'layui-input']) ?>
    <?= $form->field($model, 'password_two', [
        'template' => "{label}<div class=\"layui-input-inline\">{input}</div>",
        'labelOptions' => ['class'=>'layui-form-label'],
    ])->passwordInput(['class'=>'layui-input','lay-verify'=>'password']) ?>
</div>
<div class="layui-form-item">
    <?= $form->field($model, 'roles', [
        'labelOptions' => ['class'=>'layui-form-label'],
    ])->checkboxList($roles, ['item'=>function($index, $label, $name, $checked, $value){
        $checked=$checked?" checked":"";
        return '<input id="role_'.$index.'" type="checkbox" name="'.$name.'" value="'.$value.'" title="'.$label.'"'.$checked.'>';
    }]) ?>
</div>
<div class="layui-form-item">
    <label class="layui-form-label">权限</label>
    <div class="layui-input-inline">
        <div id="LAY-auth-tree-index"></div>
    </div>
</div>
<?php if($this->context->action->id != 'view'){?>
<div class="layui-form-item">
    <div class="layui-input-block">
        <div class="layui-footer">
            <?=Html::button('立即提交', ['class'=>'layui-btn', 'lay-submit'=>'', 'lay-filter'=>'form-submit'])?>
            <?=Html::resetButton('重置', ['class'=>'layui-btn layui-btn-primary'])?>
        </div>
    </div>
</div>
<?php }?>
<?php ActiveForm::end(); ?>
<script>
    <?php $this->beginBlock('js') ?>
    layui.use(['form','authtree'], function () {
        var form = layui.form, authtree = layui.authtree;
        var data = <?=\yii\helpers\Json::encode($permissions)?>;
        authtree.render('#LAY-auth-tree-index', data, {inputname: 'User[permissions][]', layfilter: 'lay-check-auth', openall: true});

        form.verify({
            password: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(value && value !== $('#user-password').val()){
                    return '两次密码输入不一致';
                }
            }
        });

        //监听提交
        form.on('submit(form-submit)', function(data){
            var formData = data.field;
            $.ajax({
                url:'<?=Url::to()?>',
                type:'post',
                data:formData,
                dataType:'json',
                success:function (result) {
                    layer.close(layer.index);
                    if(result.status == 200){
                        layer.alert('<div style="text-align: center">提交成功</div>', {
                            title: '提示信息',
                            yes:function(){
                                window.location.href = '<?=Url::previous() ?? Url::to()?>';
                            },
                            closeBtn:0
                        });
                    }else{
                        layer.alert('<div style="text-align: center">' + result.msg + '</div>', {
                            title: '提示信息',
                            closeBtn:0
                        });
                    }
                },
                error:function (result) {
                    layer.close(layer.index);
                    layer.alert('<div style="text-align: center">网络错误,请重试</div>', {
                        title: '提示信息',
                        closeBtn:0
                    });
                }
            });
            return false;
        });
    });
    <?php $this->endBlock() ?>
    <?php $this->registerJs($this->blocks['js'], \yii\web\View::POS_END); ?>
</script>